package android.sqlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.util.Log;

public class MyDbAdapter {
	// SQLiteOpenHelper类的构造函数有一个参数是int version，它的意思就是指数据库版本号。比如在软件1.0版本中，
	// 我们使用SQLiteOpenHelper访问数据库时，该参数为1，那么数据库版本号1就会写在我们的数据库中。
	// 到了1.1版本，我们的数据库需要发生变化，那么我们1.1版本的程序中就要使用一个大于1的整数来构造SQLiteOpenHelper类，用于访问新的数据库，比如2。
	private static final int DATABASE_VERSION = 2;

	private static final String DATABASE_NAME = "Anstop_db";
	private static final String DATABASE_TABLE = "times";

	// DB field
	public static final String KEY_TITLE = "title";
	public static final String KEY_BODY = "body";
	public static final String KEY_ROWID = "_id";

	private SQLiteDatabase mDb;
	private DataBaseHelper dbHelper;
	private Context mContext;

	private static class DataBaseHelper extends SQLiteOpenHelper {
		DataBaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			Log.i("SQL", "onCreate");
			db.execSQL("create table times (_id integer primary key autoincrement, "
					+ "title text not null, body text not null);");
		}

		// see http://blog.csdn.net/dreamer0924/article/details/7912109
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.i("SQL", "onUpgrade");
			// db.execSQL("ALTER TABLE mymessage  ADD test varchar(20)");
			onCreate(db);
		}
	}

	MyDbAdapter(Context context) {
		this.mContext = context;
	}

	public MyDbAdapter open() throws SQLException {
		dbHelper = new DataBaseHelper(mContext);
		mDb = dbHelper.getWritableDatabase();
		return this;
	}

	public void update(String title, String body, long rowId) {
		ContentValues cl = new ContentValues();
		cl.put(KEY_TITLE, title);
		cl.put(KEY_BODY, body);

		mDb.update(DATABASE_TABLE, cl, KEY_ROWID + "=" + rowId, null); // fixme:
	}

	public void close() {
		dbHelper.close();
	}

	public long createNew(String title, String body) {
		ContentValues cl = new ContentValues();
		cl.put(KEY_TITLE, title);
		cl.put(KEY_BODY, body);

		return mDb.insert(DATABASE_TABLE, null, cl);

	}

	public void empty() {
		mDb.delete(DATABASE_TABLE, null, null);
	}

	public boolean delete(long rowId) {
		return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
	}

	public Cursor fetchAll() {
		return mDb.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_TITLE,
				KEY_BODY }, null, null, null, null, null);

	}

	public Cursor fetch(long rowId) throws SQLException {
		Cursor mCursor = mDb.query(true, DATABASE_TABLE, new String[] {
				KEY_ROWID, KEY_TITLE, KEY_BODY }, KEY_ROWID + "=" + rowId,
				null, null, null, null, null);
		if (mCursor != null)
			mCursor.moveToFirst();

		return mCursor;
	}

	public void dump() {
		Cursor c = mDb.query(DATABASE_TABLE, new String[] { KEY_ROWID,
				KEY_TITLE, KEY_BODY }, null, null, null, null, null);

		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
			Log.i("SQL",
					c.getString(c.getColumnIndex(KEY_TITLE)) + " - "
							+ c.getString(c.getColumnIndex(KEY_BODY)) + " - "
							+ c.getString(c.getColumnIndex(KEY_ROWID)));
		}

	}

}
